<template>
    <div class="publicpageview currentpage">
        <div class="publicpageleft">
            <div class="listitem" v-for="item in list" :key="item.id" @click="handleItemclick(item)">
                <svg-icon :icon-class="item.icon" :color="item.id == currentActive ? '#008C93' : '#333333'"></svg-icon>
                <div class="itemname colorActive" v-if="item.id == currentActive">{{item.title}}</div>
                <div class="itemname" v-else>{{item.title}}</div>

                <div v-if="item.id == 0">
                    <div class="news" v-if="count1 > 0">{{count1}}</div>
                    <div class="newss" v-else>{{count1}}</div>
                </div>

                <div v-else>
                    <div class="news" v-if="count2 > 0">{{count2}}</div>
                    <div class="newss" v-else>{{count2}}</div>
                </div>
            </div>
        </div>


        <div class="publicpageright" v-if="currentActive == 0">

            <div class="publicformview">
                <el-form :inline="true" :model="listQuery" size="mini" ref="searchForm" label-width="100px">
                    <el-row :gutter="20">

                        <el-col :span="4">
                            <el-form-item label="标题：" prop="title">
                                <el-input v-model="listQuery.title" placeholder="请输入标题"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item label="共享人：" prop="receiverIds">
                                <el-select v-model="listQuery.receiverIds" filterable remote reserve-keyword clearable
                                    placeholder="请输入用户名" :remote-method="remoteMethod">
                                    <el-option v-for="item in userList" :key="item.uid" :label="item.username"
                                        :value="item.uid">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :span="4">
                            <div style="padding-left: 90px;">
                                <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
                                <el-button size="mini" @click="resetForm('searchForm')">重置</el-button>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <div class="publicpagerightBottom">
                <div class="listview">
                    <el-table :data="dataList" border size="mini" style="margin-top: 10px;">
                        <el-table-column align="center" label="标题" prop="subject">
                        </el-table-column>
                        <el-table-column align="center" label="共享人" prop="senderName">

                        </el-table-column>
                        <el-table-column align="center" label="发布时间" prop="sendTime">
                        </el-table-column>

                        <el-table-column align="center" label="创建日期" prop="createTime">
                        </el-table-column>

                        <el-table-column align="center" label="操作">
                            <template slot-scope="scope">
                                <el-button type="text" size="mini"
                                    @click="handleUpdate(scope.row, 'detail')">详情</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>

                <div class="publicpaginationview">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                        :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                        small></el-pagination>
                </div>
            </div>
        </div>


        <div class="publicpageright" v-if="currentActive == 1">

            <div class="publicformview">
                <el-form :inline="true" :model="listQuery2" size="mini" ref="searchForm2" label-width="100px">
                    <el-row :gutter="20">

                        <el-col :span="4">
                            <el-form-item label="标题：" prop="title">
                                <el-input v-model="listQuery2.title" placeholder="请输入标题"></el-input>
                            </el-form-item>
                        </el-col>


                        <el-col :span="4">
                            <el-form-item label="被共享人：" prop="receiverIds">
                                <el-select v-model="listQuery2.receiverIds" filterable remote reserve-keyword clearable
                                    placeholder="请输入用户名" :remote-method="remoteMethod2">
                                    <el-option v-for="item in userList2" :key="item.uid" :label="item.username"
                                        :value="item.uid">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :span="4">
                            <div style="padding-left: 90px;">
                                <el-button size="mini" type="primary" @click="handleSearch2">查询</el-button>
                                <el-button size="mini" @click="resetForm('searchForm2')">重置</el-button>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <div class="publicpagerightBottom">
                <el-button type="primary" size="mini" @click="handleAddUser('add')">添加</el-button>
                <div class="listview">
                    <el-table :data="dataList2" border size="mini" style="margin-top: 10px;">
                        <el-table-column align="center" label="标题" prop="subject">
                        </el-table-column>
                        <el-table-column align="center" label="被共享人" prop="receiverName">

                        </el-table-column>
                        <el-table-column align="center" label="发布时间" prop="sendTime">
                        </el-table-column>
                        <el-table-column align="center" label="创建日期" prop="createTime">
                        </el-table-column>

                        <el-table-column align="center" label="操作">
                            <template slot-scope="scope">
                                <el-button type="text" size="mini"
                                    @click="handleUpdate(scope.row, 'update')">编辑</el-button>
                                <el-button type="text" size="mini"
                                    @click="handleUpdate(scope.row, 'detail')">详情</el-button>
                                <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>

                <div class="publicpaginationview">
                    <el-pagination @size-change="handleSizeChange2" @current-change="handleCurrentChange2"
                        :current-page="listQuery2.pageNum" background :page-sizes="[10, 20, 50, 100]"
                        :page-size="listQuery2.pageSize" layout="total, sizes, prev, pager, next, jumper"
                        :total="total2" small></el-pagination>
                </div>
            </div>
        </div>
        <el-dialog :title="isAdd == 'add' ? '添加' :  '修改'" :visible.sync="dialogVisible" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="150px"
                size="mini">
                <el-row :gutter="20">

                    <el-col :span="24">
                        <el-form-item label="标题：" prop="subject">
                            <el-input v-model="formData.subject"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">

                        <el-form-item label="被共享人：" prop="receiverIds">
                            <el-select v-model="formData.receiverIds" multiple filterable remote reserve-keyword
                                placeholder="请输入用户名" :remote-method="remoteMethod3">
                                <el-option v-for="item in userList3" :key="item.uid" :label="item.username"
                                    :value="item.uid">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>



                    <el-col :span="24">
                        <el-form-item label="内容：" prop="content">
                            <quill-editor v-model="formData.content" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="附件：" prop="isPlanSubmission">
                            <el-upload class="upload-demo" :action="uploadAction" :on-remove="handleRemove" multiple
                                :file-list="zhanfileList" :on-success="handleUploadSuccess" :on-preview="handlePreview">
                                <el-button size="small" type="text">点击上传</el-button>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <span slot="footer" class="dialog-footer">


                <el-button type="primary" @click="handleSubmitClick" size="medium" v-if="isAdd != 'detail'">保
                    存</el-button>
                <el-button type="primary" @click="handleClose" size="medium" v-else>返 回</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import { userList } from '@/api/system'
import { queryDocumentCounts, queryDocumentreceiveList, documentsendList, createdocument, documentDelete, documentupate } from '@/api/public'
export default {
    name: 'AccommodationManagementIndex',


    data() {
        return {
            currentActive: 0,
            userStatus: "0",
            status: "0",
            probleam: "",
            fankuitext: "",
            uploadAction: 'http://125.39.77.18:9021/mall-admin/fileUpload/upload',
            zhanfileList: [],
            list: [
                {
                    id: 0,
                    title: '文档目录',
                    icon: "文档目录"
                },
                {
                    id: 1,
                    title: '我的共享',
                    icon: "我的共享"
                },


            ],
            formData: {
                subject: "",
                content: "",
                receiverIds: "",
                senderId: "",

            },
            editorOption: {
                // 配置选项
            },
            rules: {
                subject: [
                    { required: true, message: '标题不能为空', trigger: 'blur' }
                ],
                content: [
                    { required: true, message: '请编辑发送内容', trigger: 'blur' }
                ],
                receiverIds: [
                    { required: true, message: '请选择联系人', trigger: 'blur' }
                ],

            },
            userList: [],
            userList2: [],
            count1: 0,
            count2: 0,
            currentTab: 0,
            listQuery: {
                pageNum: 1,

                pageSize: 10,
                title: "",
                receiverIds: ""
            },
            total: 0,
            dataList: [],

            listQuery2: {
                pageNum: 1,
                pageSize: 10,
                title: "",
                receiverIds: ""
            },
            total2: 0,
            dataList2: [],

            isAdd: 'add',
            dialogVisible: false,
            userList3: [],

        };
    },

    mounted() {
        this.initData();
        this.remoteMethod3();
    },

    methods: {

        handleSubmitClick() {
            const { formData } = this;
            if (this.isAdd == 'add') {
                let arr = []
                if (this.zhanfileList.length > 0) {
                    this.zhanfileList.forEach(item => {
                        arr.push({ url: item.response.data.url, name: item.response.data.name })
                    })
                }
                createdocument({ ...formData, file: JSON.stringify(arr), receiverIds: this.formData.receiverIds.join(','), senderId: localStorage.getItem("userId") }).then(res => {
                    this.handleClose()
                    this.getList2();
                    this.$message.success("操作成功")
                })
            }
            if (this.isAdd == 'update') {

                let arr = []
                if (this.zhanfileList.length > 0) {
                    this.zhanfileList.forEach(item => {
                        if (item.response) {
                            arr.push({ url: item.response.data.url, name: item.response.data.name })
                        } else {
                            arr.push({ url: item.url, name: item.name })
                        }
                    })
                }
                documentupate({ ...formData, file: JSON.stringify(arr), receiverIds: this.formData.receiverIds.join(','), senderId: localStorage.getItem("userId") }).then(res => {
                    this.handleClose()
                    this.getList2();
                    this.$message.success("操作成功")

                })
            }

        },
        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;
            this.zhanfileList = []

        },
        handleAddUser(type) {
            this.isAdd = 'add';
            this.dialogVisible = true;
        },
        handleSearch() {
            this.listQuery.pageNum = 1;
            this.getList();
        },

        handleSearch2() {
            this.listQuery2.pageNum = 1;
            this.getList2();
        },


        resetForm(val) {
            this.$refs[val].resetFields();
        },

        initData() {
            queryDocumentCounts().then(res => {
                this.count1 = res.data.count1 ? res.data.count1 : 0;
                this.count2 = res.data.count2 ? res.data.count2 : 0;
            });
            this.getList();
            this.getList2();
        },
        handleUpdate(currentData, type) {
            this.isAdd = type;
            this.dialogVisible = true;
            let str = currentData.file;
            this.zhanfileList = JSON.parse(str);
            let arr = [];
            if (currentData.receiverIds) {
                currentData.receiverIds.split(',').forEach(i => {
                    arr.push(Number(i))
                });
            }
            this.$nextTick(() => {
                this.formData = { ...currentData, receiverIds: arr };
            });

        },
        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },

        handleSizeChange2(val) {
            this.listQuery2.pageNum = 1;
            this.listQuery2.pageSize = val;
            this.getList2();
        },
        handleCurrentChange2(val) {
            this.listQuery2.pageNum = val;
            this.getList2();
        },

        getList() {
            queryDocumentreceiveList({ ...this.listQuery }).then(res => {
                this.dataList = res.data.list;
                this.total = res.data.total
            })
        },

        getList2() {
            documentsendList({ ...this.listQuery2 }).then(res => {
                this.dataList2 = res.data.list;
                this.total2 = res.data.total
            })
        },
        remoteMethod(query) {
            if (query != '') {

                userList({ pageNum: 1, pageSize: 10000000, keyword: query }).then((response) => {
                    this.userList = response.data.list;
                });
            }
        },
        remoteMethod2(query) {
            if (query != '') {

                userList({ pageNum: 1, pageSize: 10000000, keyword: query }).then((response) => {
                    this.userList2 = response.data.list;
                });
            }
        },
        remoteMethod3(query) {


            userList({ pageNum: 1, pageSize: 10000000, keyword: query }).then((response) => {
                this.userList3 = response.data.list;
            });

        },


        handleUploadSuccess(response, file, fileList) {
            let arr = [];

            fileList.forEach(item => {
                if (item.response && item.response.code == 200) {
                    arr.push(item)
                } else if (item.response && item.response != 200) {
                    this.$message.error(item.name + item.response.message)
                } else {
                    arr.push(item)
                }
            })
            this.zhanfileList = arr
        },
        handleItemclick(item) {
            this.currentActive = item.id;
            this.initData();
        },
        handleRemove(file, fileList) {
            this.zhanfileList = fileList
        },

        handlePreview(file) {
            if (file.response) {
                window.open(file.response.data.url, '_blank');
            } else {
                window.open(file.url, '_blank');
            }
        },
        save() {
            this.$confirm(
                "保存成功，感谢您的反馈，我们将竭力为您服务",
                "提示",
                {
                    confirmButtonText: "关闭",
                    showCancelButton: false,
                    type: "warning",
                }
            )
                .then(() => {

                })
                .catch(() => { });
        },
        handleDelete(id) {
            this.$confirm(
                "此操作将删除该条内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    documentDelete(id).then(res => {
                        this.$message.success("操作成功")
                        this.getList2();
                    })
                })
                .catch(() => { });
        }

    },
};
</script>

<style scoped>
.currentpage {
    display: flex;
    justify-content: flex-start;
    flex-direction: row;
    /* align-items: center; */
    height: 100%;
}

.publicpageleft {
    width: 152px;
    height: 100%;
    background: #FFFFFF;
    box-sizing: border-box;
    padding: 24px 25px;
}

.listitem {
    width: 100%;
    display: flex;
    height: 16px;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    cursor: pointer;
}

.itemname {
    font-family: SourceHanSansSC, SourceHanSansSC;
    font-weight: 400;
    font-size: 14px;
    color: #333333;
    text-align: left;
    font-style: normal;
    text-transform: none;
    flex: 1;
    padding-left: 8px;
}



.colorActive {
    color: #008C93 !important;
}

.publicpageright {
    flex: 1;

    height: 100%;
    margin-left: 8px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;

    box-sizing: border-box;

}

.bangzhuzhongxin {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 24px;
}

/* 
.el-select {
    width: 30%;
} */

.inputview {
    width: 100%;
    margin-top: 24px;
}

.buttonview {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 24px;
}

/* ::v-deep .el-input--mini {
    width: 500px;
} */

.news {
    width: 16px;
    height: 16px;
    background: #FF4C4F;
    border-radius: 8px 8px 8px 8px;
    text-align: center;
    line-height: 16px;
    color: #FFFFFF;
    font-weight: 400;
    font-size: 12px;

}

.newss {
    width: 16px;
    height: 16px;
    background: #CCCCCC;
    border-radius: 8px 8px 8px 8px;
    text-align: center;
    line-height: 16px;
    color: #ffffff;
    font-weight: 400;
    font-size: 12px;
}

.publicpagerightBottom {
    flex: 1;
    margin-top: 8px;
    background: #FFFFFF;
    box-sizing: border-box;
    padding: 12px;
}

.publicpagerightBottomTop {
    width: 100%;
    height: 40px;
    border-bottom: 1px solid #D9D9D9;
    display: flex;
    align-items: center;
    justify-content: flex-start;
}

.activetabItem {
    width: 100px;
    height: 40px;
    background: #008C93;
    border-radius: 4px 4px 0px 0px;
    line-height: 40px;

    font-family: SourceHanSansSC, SourceHanSansSC;
    font-weight: 400;
    font-size: 16px;
    color: #FFFFFF;
    text-align: left;
    font-style: normal;
    text-transform: none;
    text-align: center;
    margin-right: 8px;
    cursor: pointer;
}

.tabItem {
    width: 100px;
    height: 40px;
    background: #EFF2F7;
    border-radius: 4px 4px 0px 0px;
    line-height: 40px;

    font-family: SourceHanSansSC, SourceHanSansSC;
    font-weight: 400;
    font-size: 16px;
    color: #333333;
    text-align: left;
    font-style: normal;
    text-transform: none;
    text-align: center;
    margin-right: 8px;
    cursor: pointer;
}

.listview {
    width: 100%;
}

.listItem {
    width: 100%;
    height: 80px;
    border-bottom: 1px solid #D9D9D9;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.listItemTitle {
    font-family: SourceHanSansSC, SourceHanSansSC;
    font-weight: 400;
    font-size: 14px;
    color: #333333;
    line-height: 14px;
    text-align: left;
    font-style: normal;
    text-transform: none;
}

.listItemTime {

    font-family: SourceHanSansSC, SourceHanSansSC;
    font-weight: 400;
    font-size: 14px;
    color: #999999;
    line-height: 14px;
    text-align: left;
    font-style: normal;
    text-transform: none;
    margin-top: 8px;
}

.shezhiyidu {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);

    font-family: SourceHanSansSC, SourceHanSansSC;
    font-weight: 400;
    font-size: 14px;
    color: #008C93;

    font-style: normal;
    text-transform: none;
    cursor: pointer;
}
</style>